/**
 *@category 
 *@author 邱磊
 *@date 2015年11月13日 下午2:53:16
 */
package com.hsnn.medstgmini.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hsnn.medstgmini.util.form.FileLoadForm;

/**
 *@category 
 *@author 邱磊
 *@date 2015年11月13日 下午2:53:16
 *@param 
 */
public class FileUtil {
	private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);
	public static void downLoad(FileLoadForm form) throws Exception{  
		HttpServletResponse response = form.getResponse();
		HttpServletRequest request = form.getRequest();
        response.setContentType("text/html;charset=UTF-8");   
        BufferedInputStream in = null;  
        BufferedOutputStream out = null;  
        request.setCharacterEncoding("UTF-8");  
        String filePath = form.getFilePath() + form.getFileName();
        try {  
            File f = new File(filePath);  
            response.setContentType("application/x-excel");  
            response.setCharacterEncoding("UTF-8");  
            response.setHeader("Content-Disposition", "attachment; filename="+form.getFileName());  
            response.setHeader("Content-Length",String.valueOf(f.length()));  
            in = new BufferedInputStream(new FileInputStream(f));  
            out = new BufferedOutputStream(response.getOutputStream());  
            byte[] data = new byte[1024];  
            int len = 0;  
            while (-1 != (len=in.read(data, 0, data.length))) {  
                out.write(data, 0, len);  
            }  
        } catch (Exception e) {  
        	 logger.error("IOStream error !", e);
        } finally {  
            if (in != null) {  
                in.close();  
            }  
            if (out != null) {  
                out.close();  
            }  
        }  
	}
}
